package com.lzx.adx.report.util;

import com.alibaba.fastjson.JSONObject;
import com.lzx.adx.report.dto.AccountDTO;
import com.lzx.adx.report.dto.ReportDTO;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

public class MongacUtil {

    /**
     * 处理地址对接
     * @param reportDTO
     * @param accountDTO
     * @return
     */
    public static String handUrl(ReportDTO reportDTO, AccountDTO accountDTO) {

        String extend = accountDTO.getDspExtend();
        JSONObject jsonObject = JSONObject.parseObject(extend);
        String accessId = jsonObject.getString("access_id");
        String secret = accountDTO.getDspSalt();

        long ts=System.currentTimeMillis()/1000;

        String src="access_id="+accessId+"&ts="+ts;
        String sign = StringUtils.toRootLowerCase(Md5Util.MD5(src+secret)) ;

        Map<String,String> params=new HashMap();
        params.put("REQUEST_ID",reportDTO.getBizRequestId());
        params.put("IMEI",reportDTO.getImei());
        params.put("IMEI_MD5",reportDTO.getImei());
        params.put("OAID",reportDTO.getOaid());
        params.put("OAID_MD5","");
        params.put("IDFA","");
        params.put("IDFA_MD5","");
        params.put("ANDROID_ID_MD5",reportDTO.getHash_android_id());
        params.put("ADVERTISER_ID","");
        params.put("MAC_MD5","");
        params.put("DEEP_LINK","");
        params.put("TS",String.valueOf(ts));
        params.put("OS","0");
        params.put("IP",reportDTO.getIp());
        params.put("IP_MD5","");
        params.put("UA",URLEncoderDecoderTool.encodeURL(reportDTO.getUser_agent()));

        String allCabackUrl = String.format(accountDTO.getDspCallbackUrl(), reportDTO.getBizRequestId());

        params.put("CALLBACK_URL",allCabackUrl);
        String url = MacroReplacement.replaceMacros(accountDTO.getDspReportUrl(), params);

        return  url+"&signature="+sign;
    }
}
